package javazoom.jlGui.skin;

/**
 * activeComponent.
 *-----------------------------------------------------------------------
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

import java.awt.*;
import java.awt.event.*;

/**
 * activeComponent is used to manage two state graphical components.
 *
 * @author	E.B from JavaZOOM
 *
 * Homepage : http://www.javazoom.net
 *
 */
public class activeComponent extends Canvas
{
	private Image           releasedImage;
    private Image           pressedImage;
    private long            actionMask;
    private boolean         pressed = false;
    private int             mouseX, mouseY;
    private int             button;
    private ActionListener  actionListener;
    private String          actionCommand;

    /**
     * Contructor that create the active Component with 2 images.
     */
    public activeComponent(Image releasedImage, Image pressedImage, long theMask)
    {
        this.releasedImage = releasedImage;
        this.pressedImage = pressedImage;
        this.actionMask = theMask;
        setSize(releasedImage.getWidth(null), releasedImage.getHeight(null) );
        enableEvents( theMask );
    }

    public void paint( Graphics g )
    {
        g.setColor(Color.black);
        int width = getSize().width, height = getSize().height;
        if (pressed == false) g.drawImage( releasedImage, 0, 0, width, height, this );
        else g.drawImage( pressedImage, 0, 0, width, height, this );
    }

    public Dimension getPreferredSize()
    {
        return getSize();
    }

    public void processEvent( AWTEvent e )
    {
        button = ((MouseEvent)e).getModifiers();
        if ( e.getID() == MouseEvent.MOUSE_PRESSED )
        {
             pressed = true;
             repaint();
        }
        else if ( e.getID() == MouseEvent.MOUSE_RELEASED )
        {
             pressed = false;
             repaint();
             fireEvent();
        }
        else if ( e.getID() == MouseEvent.MOUSE_DRAGGED )
        {
             mouseX = ((MouseEvent)e).getX();
             mouseY = ((MouseEvent)e).getY();
             pressed = true;
             repaint();
             fireEvent();
        }
        super.processEvent(e);
     }

    public void setActionCommand( String actionCommand )
    {
        this.actionCommand = actionCommand;
    }

    public void addActionListener(ActionListener l)
    {
        actionListener = AWTEventMulticaster.add(actionListener, l);
    }

    public void removeActionListener(ActionListener l)
    {
        actionListener = AWTEventMulticaster.remove(actionListener, l);
    }

    public void fireEvent()
    {
        if (actionListener != null)
        {
            ActionEvent event = new ActionEvent( this, ActionEvent.ACTION_PERFORMED, actionCommand );
            actionListener.actionPerformed( event );
         }
    }

    public final int getMouseX()
    {return mouseX;}

    public final int getMouseY()
    {return mouseY;}

    public final boolean isMousePressed()
    {return pressed;};

    public final int getMouseButton()
    {return button;};

}
